<template>
	<div class="message_type_add_edit add_edit">
		<el-form v-loading="formLoading" :model="form" :rules="rules" ref="ruleForm" label-width="100px">
			<el-form-item label="类型" prop="type" ref="type">
				<el-radio-group :disabled="addBtnLoading" v-model="form.type">
					<el-radio :label="1">管理员</el-radio>
					<el-radio :label="2">用户</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="标题" prop="title" ref="title">
				<el-input :disabled="addBtnLoading" v-model="form.title" placeholder="请输入标题" clearable></el-input>
			</el-form-item>
			<el-form-item label="消息ID" prop="message_id" ref="message_id">
				<el-input :disabled="addBtnLoading" v-model="form.message_id" placeholder="请输入消息ID" clearable>
				</el-input>
			</el-form-item>
			<el-form-item label="微信模板" prop="wx_template_id" ref="wx_template_id">
				<el-select :disabled="addBtnLoading" v-model="form.wx_template_id" placeholder="请选择" filterable
					:loading="templateLoading" loading-text="加载中..." clearable>
					<el-option v-for="item in templateOptions" :key="item.template_id" :label="item.title"
						:value="item.template_id">
						<el-tooltip effect="light" placement="left" :enterable="false">
							<pre slot="content">
								<div v-if="item.content">
									<div class="margin_l_10">数据</div>
									<div v-html="item.content" style="background-color: #f5f5f5;border-radius: 10px;" class="margin_t_5 padding_10"></div>
								</div>
								<div v-if="item.example">
									<div class="margin_t_10 margin_l_10">示例</div>
									<div v-html="item.example" style="background-color: #f5f5f5;border-radius: 10px;" class="margin_t_5 padding_10"></div>
								</div>
							</pre>
							<div>{{item.title}}</div>
						</el-tooltip>
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="描述">
				<el-input :disabled="addBtnLoading" v-model="form.description" placeholder="请输入描述" clearable></el-input>
			</el-form-item>
			<el-form-item label="排序">
				<el-input :disabled="addBtnLoading" type="number" v-model.number="form.sort" :min="0"
					placeholder="请输入排序" clearable></el-input>
			</el-form-item>
			<el-form-item label="状态">
				<el-switch :disabled="addBtnLoading" v-model="form.state" active-color="#ff6a6c" inactive-color="#bbb"
					:active-value="1" :inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item>
				<el-button :loading="addBtnLoading" round icon="el-icon-check" class="confirm-btn"
					@click="submitForm('ruleForm')">提交</el-button>
				<el-button :disabled="addBtnLoading" round icon="el-icon-back" class="return-btn" @click="returnPage">返回
				</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	const __name = 'wmzmall-message-type';
	export default {
		props: {
			id: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				formLoading: false,
				form: {
					type: '',
					title: '',
					message_id: '',
					wx_template_id: '',
					wx_template: {},
					uids: [],
					users: [],
					description: '',
					sort: 0,
					state: 1
				},
				templateLoading: false,
				templateOptions: [],
				rules: {
					type: [{
						required: true,
						message: '请选择类型',
						trigger: 'change'
					}],
					title: [{
						required: true,
						message: '请输入标题',
						trigger: 'change'
					}],
					message_id: [{
						required: true,
						message: '请输入消息ID',
						trigger: 'change'
					}],
					wx_template_id: [{
						required: true,
						message: '请选择微信模板',
						trigger: 'change'
					}]
				},
				addBtnLoading: false
			};
		},
		methods: {
			async loadData() {
				this.templateLoading = true;
				this.$func.wmzadmin
					.call('message/wxTemplateGet', {},
						false
					).then(res => {
						if (res.code == 200) {
							this.templateOptions = res.datas;
						}
						this.templateLoading = false;
					});

				if (!this.id) {
					this.$db[__name].getsort().then(res => {
						if (res.code == 200) {
							this.form.sort = ++res.datas.sort;
						}
					});
					return;
				}

				this.formLoading = true;

				await this.$db[__name].tofirst({
					_id: this.id,
					loading: false
				}).then(res => {
					if (res.code == 200) {
						for (let item in this.form) {
							this.form[item] = res.datas[item];
						}
					}
					this.formLoading = false;
				});
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					delete obj.type
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						this.form.wx_template = this.templateOptions.find(x => x.template_id == this.form
							.wx_template_id);

						this.addBtnLoading = true;
						if (!this.id) {
							this.$db[__name].add(this.form, false).then(res => {
								if (res.code == 200) {
									this.returnPage();
									this.$emit('loadData');
								}
								this.addBtnLoading = false;
							});
						} else {
							this.$db[__name].update(this.id, this.form, false).then(res => {
								if (res.code == 200) {
									this.returnPage();
									this.$emit('loadData');
								}
								this.addBtnLoading = false;
							});
						}
					}
				});
			},
			reset() {
				if (this.$refs.ruleForm) {
					this.$refs.ruleForm.resetFields();
				}
			},
			returnPage() {
				this.$emit('closeDrawer');
			}
		},
		created() {
			this.loadData();
		}
	};
</script>

<style>
	.message_type_add_edit>>>.el-select {
		width: 60%;
	}

	.message_type_add_edit>>>.el-input {
		width: 90%;
	}
</style>
